/*******************************************************************************
* REPLICATION CODE FOR:
* "Silent Suffering: Depression and Political Trust in China"
* Authors: Xing Chen, Xiaoxiao Shen
* Corresponding Author: Xing Chen
* Journal: Research and Politics
*
* This script replicates all tables and figures from the main manuscript and appendix.
* Date of Code Refinement: 25-Jul-2024
*******************************************************************************/

clear all
set more off

* USER MUST DEFINE THE ROOT DIRECTORY HERE !!!
global root "/path/to/your/main/project/folder" // 
* Define global paths for sub-directories
global data    "$root/Data"
global tables  "$root/Tables"
global figures "$root/Figures"
global log     "$root/Log"

* Start a log file to record output
cap log close
log using "$log/replication_log.log", replace

// --- 1. LOAD DATA AND PREPARE VARIABLES ---

* Load the final analysis dataset
use "$data/main_data.dta", clear

* Rename key variables for clarity
rename p_mhealth               depression
rename politrust               political_trust
rename p_mh_mild_depression    depression_mild
rename p_mh_mo_depression      depression_moderate
rename p_mh_severe_depression  depression_severe
rename p_unfair_ineq           perceived_income_inequality
rename p_unfair_huji           perceived_hukou_inequality
rename p_unfair_gender         perceived_gender_inequality
rename p_home_status           perceived_social_status
rename p_workplace_dummy       public_sector // 1 if works in public sector
rename p_info_net_dummy        internet_is_major_source

* Define global lists of variables for cleaner code
global controls_indiv   p_male p_age p_age_square p_urban p_marriage_dummy p_edu_year p_work hh_inc p_ccp
global controls_city    city_pop city_pgdp
global all_controls     $controls_indiv $controls_city

// --- 2. GENERATE SUMMARY STATISTICS ---
* *** TABLE A1 (APPENDIX): SUMMARY STATISTICS ***
global dep_vars         political_trust mmx_p_trust_parent mmx_p_trust_neighbour mmx_p_trust_us mmx_p_trust_stranger mmx_p_trust_doctor
global indep_vars       depression depression_mild depression_moderate depression_severe
global moderator_vars   mmx_p_aware_ine
global all_vars_summary $dep_vars $indep_vars $moderator_vars $controls_indiv $controls_city

* Create the summary statistics table and export to Word
asdoc sum `all_vars_summary', save("$tables/Table_A1_Summary_Statistics.doc") replace ///
    title("Table A1: Summary Statistics") stat(N mean sd min max) dec(3)
    
// --- 3. MAIN ANALYSIS: BASELINE RESULTS ---

* *** TABLE 1 & TABLE A2 (APPENDIX): BASELINE RESULTS ***
* Table A2 shows full results; Table 1 is a condensed version.
eststo clear
eststo m1: reghdfe political_trust depression, absorb(pid year) cluster(fid)
eststo m2: reghdfe political_trust depression $controls_indiv, absorb(pid year) cluster(fid)
eststo m3: reghdfe political_trust depression $all_controls, absorb(pid year) cluster(fid)

* Export full results for Table A2
esttab m1 m2 m3 using "$tables/Table_A2_Baseline_Full.rtf", replace ///
    b(3) se(3) ar2(%9.4f) star(* .10 ** .05 *** .01) ///
    title("Table A2: Baseline Effect of Depression on Political Trust (Full)") ///
    stats(N adj. R2, fmt(%9.0f %9.4f) labels("Observations" "Adj. R-squared")) nogap

* Export condensed results for Table 1
esttab m1 m2 m3 using "$tables/Table_1_Baseline_Condensed.rtf", replace ///
    b(3) se(3) ar2(%9.4f) star(* .10 ** .05 *** .01) keep(depression) ///
    addnotes("Individual-level controls included in Models 2 and 3.") ///
    addnotes("City-level controls included in Model 3.") ///
    title("Table 1: Baseline Effect of Depression on Political Trust") ///
    stats(N adj. R2, fmt(%9.0f %9.4f) labels("Observations" "Adj. R-squared")) nogap


// --- 4. HETEROGENEITY AND MECHANISM ANALYSIS ---
* *** FIGURE 1 & TABLE A3 (APPENDIX): EFFECT BY DEPRESSION SEVERITY ***
eststo clear
eststo m_mild:    reghdfe political_trust depression_mild $all_controls, absorb(pid year) cluster(fid)
eststo m_moderate: reghdfe political_trust depression_moderate $all_controls, absorb(pid year) cluster(fid)
eststo m_severe:   reghdfe political_trust depression_severe $all_controls, absorb(pid year) cluster(fid)

* Export regression table for Appendix A3
esttab m_mild m_moderate m_severe using "$tables/Table_A3_Depression_Severity.rtf", replace ///
    b(3) se(3) ar2(%9.4f) star(* .10 ** .05 *** .01) drop(_cons $all_controls) ///
    title("Table A3: Effect of Depression Severity on Political Trust") ///
    stats(N adj. R2, fmt(%9.0f %9.4f) labels("Observations" "Adj. R-squared")) nogap

* Create coefficient plot for Figure 1
coefplot (m_mild, label("Mild Depression")) ///
         (m_moderate, label("Moderate Depression")) ///
         (m_severe, label("Severe Depression")), ///
    drop(_cons) vertical keep(*depre*) ///
    title("Figure 1: Effect of Depression on Political Trust, by Severity") ///
    ytitle("Coefficient on Political Trust") ///
    xline(0, lpattern(dash) lcolor(black*0.5)) ///
    legend(position(6) rows(1))
graph save "$figures/Figure_1_Depression_Severity.gph", replace


* *** FIGURE 2 & TABLE A4 (APPENDIX): EFFECT ON TRUST IN OTHER ENTITIES ***
eststo clear
eststo t_social:   reghdfe mmx_p_trust_social depression $all_controls, absorb(pid year) cluster(fid)
eststo t_parent:   reghdfe mmx_p_trust_parent depression $all_controls, absorb(pid year) cluster(fid)
eststo t_neighbor: reghdfe mmx_p_trust_neighbour depression $all_controls, absorb(pid year) cluster(fid)
eststo t_us:       reghdfe mmx_p_trust_us depression $all_controls, absorb(pid year) cluster(fid)
eststo t_stranger: reghdfe mmx_p_trust_stranger depression $all_controls, absorb(pid year) cluster(fid)
eststo t_doctor:   reghdfe mmx_p_trust_doctor depression $all_controls, absorb(pid year) cluster(fid)

* Export regression table for Appendix A4
esttab t_social t_parent t_neighbor t_us t_stranger t_doctor using "$tables/Table_A4_Other_Trust.rtf", replace ///
    b(3) se(3) ar2(%9.4f) star(* .10 ** .05 *** .01) keep(depression) ///
    collabels("Social" "Parents" "Neighbors" "Americans" "Strangers" "Doctors") ///
    title("Table A4: Effect of Depression on Trust in Other Entities") ///
    stats(N adj. R2, fmt(%9.0f %9.4f) labels("Observations" "Adj. R-squared")) nogap

* Create coefficient plot for Figure 2
coefplot (t_social, rename(depression = "General Social Trust")) ///
         (t_parent, rename(depression = "Trust in Parents")) ///
         (t_neighbor, rename(depression = "Trust in Neighbors")) ///
         (t_us, rename(depression = "Trust in US Citizens")) ///
		 (t_stranger, rename(depression = "Trust in Strangers")) ///
         (t_doctor, rename(depression = "Trust in Doctors")), ///
    drop(_cons) horizontal recast(scatter) ///
    title("Figure 2: Effect of Depression on Trust in Other Entities") ///
    xline(0, lpattern(dash) lcolor(black*0.5)) yscale(reverse) legend(off)
graph save "$figures/Figure_2_Other_Trust.gph", replace


* *** FIGURE 3 & TABLE A5 (APPENDIX): HETEROGENEITY BY AGE ***
* Create age groups based on respondent's age in the first wave they appear
sort pid year
by pid: egen first_year_flag = tag(pid)
gen age_at_first_survey = p_age if first_year_flag == 1
by pid: egen age_group_base = max(age_at_first_survey)
recode age_group_base (15/25=1 "15-25") (26/40=2 "26-40") (41/55=3 "41-55") (56/110=4 "56+"), gen(age_group)
by pid: egen final_age_group = max(age_group) // Assign a fixed age group to each person

eststo clear
eststo age_full: reghdfe political_trust depression $all_controls, absorb(pid year) cluster(fid)
eststo age_g1:   reghdfe political_trust depression $all_controls if final_age_group == 1, absorb(pid year) cluster(fid)
eststo age_g2:   reghdfe political_trust depression $all_controls if final_age_group == 2, absorb(pid year) cluster(fid)
eststo age_g3:   reghdfe political_trust depression $all_controls if final_age_group == 3, absorb(pid year) cluster(fid)
eststo age_g4:   reghdfe political_trust depression $all_controls if final_age_group == 4, absorb(pid year) cluster(fid)

* Export regression table for Appendix A5
esttab age_full age_g1 age_g2 age_g3 age_g4 using "$tables/Table_A5_Heterogeneity_Age.rtf", replace ///
    b(3) se(3) ar2(%9.4f) star(* .10 ** .05 *** .01) keep(depression) ///
    collabels("Full Sample" "15-25" "26-40" "41-55" "56+") ///
    title("Table A5: Heterogeneity by Age Group") ///
    stats(N adj. R2, fmt(%9.0f %9.4f) labels("Observations" "Adj. R-squared")) nogap

* Create interaction plots for Figure 3 (Note: interflex can be slow)
* For Males
interflex political_trust depression p_age $controls_city $controls_indiv if p_male==1, fe(pid year) vce(cluster fid) ///
    title("Males") ytitle("Marginal Effect of Depression") xtitle("Age")
graph save "$figures/Figure_3_Age_Male.gph", replace

* For Females
interflex political_trust depression p_age $controls_city $controls_indiv if p_male==0, fe(pid year) vce(cluster fid) ///
    title("Females") ytitle("Marginal Effect of Depression") xtitle("Age")
graph save "$figures/Figure_3_Age_Female.gph", replace

* Combine graphs for final figure
graph combine "$figures/Figure_3_Age_Male.gph" "$figures/Figure_3_Age_Female.gph", ///
    title("Figure 3: Marginal Effect of Depression on Political Trust, by Age and Gender")
graph save "$figures/Figure_3_Combined.gph", replace


* *** FIGURE 4 & TABLE A6 (APPENDIX): HETEROGENEITY BY OTHER CHARACTERISTICS ***
* Create time-invariant dummy variables for heterogeneity analysis
* This method assigns a permanent characteristic to a person based on their status in the first wave they appear.
recode p_edu_year (0/8=0 "Below High School") (9/max=1 "High School or Above"), gen(high_school_or_above)
foreach var in p_male high_school_or_above p_work public_sector p_net internet_is_major_source {
    by pid (year): gen `var'_fixed = `var'[1]
}

eststo clear
eststo h_female: reghdfe political_trust depression $all_controls if p_male_fixed == 0, absorb(pid year) cluster(fid)
eststo h_male:   reghdfe political_trust depression $all_controls if p_male_fixed == 1, absorb(pid year) cluster(fid)
eststo h_low_edu:reghdfe political_trust depression $all_controls if high_school_or_above_fixed == 0, absorb(pid year) cluster(fid)
eststo h_high_edu:reghdfe political_trust depression $all_controls if high_school_or_above_fixed == 1, absorb(pid year) cluster(fid)
eststo h_unemployed:reghdfe political_trust depression $all_controls if p_work_fixed == 0, absorb(pid year) cluster(fid)
eststo h_employed:reghdfe political_trust depression $all_controls if p_work_fixed == 1, absorb(pid year) cluster(fid)
eststo h_private:reghdfe political_trust depression $all_controls if public_sector_fixed == 0, absorb(pid year) cluster(fid)
eststo h_public: reghdfe political_trust depression $all_controls if public_sector_fixed == 1, absorb(pid year) cluster(fid)
eststo h_no_net: reghdfe political_trust depression $all_controls if p_net_fixed == 0, absorb(pid year) cluster(fid)
eststo h_net:    reghdfe political_trust depression $all_controls if p_net_fixed == 1, absorb(pid year) cluster(fid)
eststo h_net_not_major: reghdfe political_trust depression $all_controls if internet_is_major_source_fixed == 0, absorb(pid year) cluster(fid)
eststo h_net_major: reghdfe political_trust depression $all_controls if internet_is_major_source_fixed == 1, absorb(pid year) cluster(fid)

* Export regression table for Appendix A6
esttab h_female h_male h_low_edu h_high_edu h_unemployed h_employed h_private h_public h_no_net h_net h_net_not_major h_net_major ///
    using "$tables/Table_A6_Heterogeneity_Other.rtf", replace b(3) se(3) ar2(%9.4f) star(* .10 ** .05 *** .01) keep(depression) ///
    title("Table A6: Heterogeneity by Other Characteristics") stats(N, fmt(%9.0f)) nogap

* Create coefficient plot for Figure 4
coefplot (h_female, rename(depression = "Female")) ///
         (h_male, rename(depression = "Male")) ///
         (h_low_edu, rename(depression = "Education: Below High School")) ///
         (h_high_edu, rename(depression = "Education: High School or Above")) ///
		 (h_unemployed, rename(depression = "Unemployed")) ///
         (h_employed, rename(depression = "Employed")) ///
         (h_private, rename(depression = "Not Public Sector")) ///
         (h_public, rename(depression = "Public Sector")) ///
		 (h_no_net, rename(depression = "No Internet Access")) ///
         (h_net, rename(depression = "Internet Access")) ///
         (h_net_not_major, rename(depression = "Internet Not Major Source")) ///
         (h_net_major, rename(depression = "Internet is Major Source")), ///
    drop(_cons) horizontal recast(scatter) title("Figure 4: Heterogeneity by Other Characteristics") ///
    xline(0, lpattern(dash) lcolor(black*0.5)) yscale(reverse) legend(off)
graph save "$figures/Figure_4_Heterogeneity_Other.gph", replace


* *** FIGURE 5 & TABLE A7 (APPENDIX): HETEROGENEITY BY EXPERIENCE WITH GOVERNMENT ***
eststo clear
eststo g_no_unfair: reghdfe political_trust depression $all_controls if p_unfair_gov == 0, absorb(pid year) cluster(fid)
eststo g_unfair:    reghdfe political_trust depression $all_controls if p_unfair_gov == 1, absorb(pid year) cluster(fid)
eststo g_no_conflict:reghdfe political_trust depression $all_controls if p_conflict_gov == 0, absorb(pid year) cluster(fid)
eststo g_conflict:  reghdfe political_trust depression $all_controls if p_conflict_gov == 1, absorb(pid year) cluster(fid)
eststo g_no_delay:   reghdfe political_trust depression $all_controls if p_dlay_gov == 0, absorb(pid year) cluster(fid)
eststo g_delay:      reghdfe political_trust depression $all_controls if p_dlay_gov == 1, absorb(pid year) cluster(fid)
eststo g_no_charge:  reghdfe political_trust depression $all_controls if p_chag_gov == 0, absorb(pid year) cluster(fid)
eststo g_charge:     reghdfe political_trust depression $all_controls if p_chag_gov == 1, absorb(pid year) cluster(fid)

* Export regression table for Appendix A7
esttab g_no_unfair g_unfair g_no_conflict g_conflict g_no_delay g_delay g_no_charge g_charge ///
    using "$tables/Table_A7_Heterogeneity_Gov_Experience.rtf", replace b(3) se(3) ar2(%9.4f) star(* .10 ** .05 *** .01) keep(depression) ///
    title("Table A7: Heterogeneity by Experience with Government") stats(N, fmt(%9.0f)) nogap

* Create coefficient plot for Figure 5
coefplot (g_no_unfair, rename(depression = "Not Unfairly Treated")) ///
         (g_unfair, rename(depression = "Unfairly Treated")) ///
         (g_no_conflict, rename(depression = "No Conflict with Gov")) ///
         (g_conflict, rename(depression = "Conflict with Gov")) ///
		 (g_no_delay, rename(depression = "Not Delayed by Gov")) ///
         (g_delay, rename(depression = "Delayed by Gov")) ///
         (g_no_charge, rename(depression = "Not Unreasonably Charged")) ///
         (g_charge, rename(depression = "Unreasonably Charged")), ///
    drop(_cons) horizontal recast(scatter) title("Figure 5: Heterogeneity by Experience with Government") ///
    xline(0, lpattern(dash) lcolor(black*0.5)) yscale(reverse) legend(off)
graph save "$figures/Figure_5_Gov_Experience.gph", replace


* *** TABLE 2 & TABLE A8 (APPENDIX): MODERATING EFFECT OF INEQUALITY PERCEPTION ***
* Create interaction terms
gen dep_X_income_ineq = depression * perceived_income_inequality
gen dep_X_hukou_ineq  = depression * perceived_hukou_inequality
gen dep_X_gender_ineq = depression * perceived_gender_inequality
gen dep_X_soc_status  = depression * perceived_social_status

eststo clear
eststo mod_income: reghdfe political_trust depression c.depression#c.perceived_income_inequality $all_controls, absorb(pid year) cluster(fid)
eststo mod_hukou:  reghdfe political_trust depression c.depression#c.perceived_hukou_inequality $all_controls, absorb(pid year) cluster(fid)
eststo mod_gender: reghdfe political_trust depression c.depression#c.perceived_gender_inequality $all_controls, absorb(pid year) cluster(fid)
eststo mod_status: reghdfe political_trust depression c.depression#c.perceived_social_status $all_controls, absorb(pid year) cluster(fid)

* Export regression table for Table 2 and Appendix A8
esttab mod_income mod_hukou mod_gender mod_status using "$tables/Table_2_and_A8_Moderators.rtf", replace ///
    b(3) se(3) ar2(%9.4f) star(* .10 ** .05 *** .01) keep(depression c.depression#*) ///
    rename(c.depression#c.perceived_income_inequality "depression*income inequality" ///
           c.depression#c.perceived_hukou_inequality "depression*Hukou inequality" ///
           c.depression#c.perceived_gender_inequality "depression*gender inequality" ///
           c.depression#c.perceived_social_status "depression*perceived social status") ///
    collabels("Income" "Hukou" "Gender" "Social Status") ///
    title("Table 2: Moderating Effect of Perceived Inequality") ///
    stats(N adj. R2, fmt(%9.0f %9.4f) labels("Observations" "Adj. R-squared")) nogap


// --- 5. ROBUSTNESS CHECKS ---

* *** TABLE A9 (APPENDIX): ORDERED LOGIT REGRESSION ***
* Note: Ordered logit does not support individual fixed effects in the same way.
* This is a pooled OLS model for robustness.
eststo clear
eststo ologit1: ologit political_trust depression, cluster(fid)
eststo ologit2: ologit political_trust depression $controls_indiv, cluster(fid)
eststo ologit3: ologit political_trust depression $all_controls, cluster(fid)

esttab ologit1 ologit2 ologit3 using "$tables/Table_A9_Robustness_Ologit.rtf", replace ///
    b(3) se(3) star(* .10 ** .05 *** .01) ///
    title("Table A9: Robustness Check with Ordered Logit") ///
    stats(N, fmt(%9.0f) labels("Observations")) nogap

* *** TABLE A10 (APPENDIX): SAMPLE RESTRICTIONS ***

* Create flags for sample restrictions
* Flag for extreme weather
gen extreme_weather = (weather_wind >= 99 | weather_temp <= 1 | weather_temp >= 99)
* Flag for cities with US consulates (Beijing, Shanghai, Chengdu, Guangzhou, Shenyang)
gen has_us_consulate = (city_name == "上海市" | city_name == "北京市" | city_name == "成都市" | city_name == "广州市" | city_name == "沈阳市")
* Note: This check relies on Chinese characters in the 'city_name' variable. 
* A more robust method would use standardized city codes if available.

* Flag for individuals who have migrated
by pid: egen ever_migrated_prov = max(p_move_prov)
by pid: egen ever_migrated_city = max(p_move_city)

eststo clear
eststo rob_weather: reghdfe political_trust depression $all_controls if extreme_weather == 0, absorb(pid year) cluster(fid)
eststo rob_consulate: reghdfe political_trust depression $all_controls if has_us_consulate == 0, absorb(pid year) cluster(fid)
eststo rob_mig_prov: reghdfe political_trust depression $all_controls if ever_migrated_prov == 0, absorb(pid year) cluster(fid)
eststo rob_mig_city: reghdfe political_trust depression $all_controls if ever_migrated_city == 0, absorb(pid year) cluster(fid)

esttab rob_weather rob_consulate rob_mig_prov rob_mig_city using "$tables/Table_A10_Robustness_Subsamples.rtf", replace ///
    b(3) se(3) ar2(%9.4f) star(* .10 ** .05 *** .01) keep(depression) ///
    collabels("Excl. Extreme Weather" "Excl. Consulate Cities" "Excl. Migrants (Prov)" "Excl. Migrants (City)") ///
    title("Table A10: Robustness Checks with Sample Restrictions") ///
    stats(N adj. R2, fmt(%9.0f %9.4f) labels("Observations" "Adj. R-squared")) nogap

